#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 20000

static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void * function_add(void * arg){
    pthread_mutex_lock(&mutex);
    int *a = (int *)arg;
    *a = *a + 1;
    pthread_mutex_unlock(&mutex);
    return NULL;
}

int main(int argc, char const *argv[])
{   
    int a = 0;
    pthread_t threads[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; i++)   
    {
        pthread_create(&threads[i], NULL, function_add, (void *)&a);
    }

    //等待所有线程执行完毕
    for (int i = 0; i < NUM_THREADS; i++)   
    {
        pthread_join(threads[i], NULL);
    }

    printf("a = %d\n", a);
    
    
    return 0;
}
